package JMH.day03.atomicfieldupdater;

import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;

public class AtomicFieldUpdaterExample {

    public static void main(String[] args) {

        AtomicIntegerFieldUpdater<Alex> salary = AtomicIntegerFieldUpdater.newUpdater(Alex.class, "salary");
        Alex alex = new Alex();
        if (salary.compareAndSet(alex, 0, 1)) {
            System.out.println("更新alex成功");
            System.out.println(alex);
        } else {
            System.out.println("更新alex失败");
        }

    }

}

class Alex {
    volatile int salary;

    public int getSalary() {
        return salary;
    }

    @Override
    public String toString() {
        return "Alex{" +
                "salary=" + salary +
                '}';
    }
}